﻿/**********************************************
 * 功能名称：产品领料数据导入后，设定作业中心
编写人：  杨莹
建立时间： 杨 2009年3月3日
说明：
加载页面时，显示所有集成过来的产品领料数据
 *用户可对作业中心ID进行定义。并导入材料消耗表。
 *设定好所有的产品领料的作业中心后，导入材料消耗表。

 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Collections.Generic;

namespace Hisun.View.Meterial
{
    public partial class Meterial_P_CenterSet : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();

        public string alert;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadioButtonList_Query.Items[0].Selected = true;

                int Totals = TotalPageAmount(RadioButtonList_Query.SelectedItem.Value);
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1页，转到";
                BindMyData(0, RadioButtonList_Query.SelectedItem.Value);





            }
        }



        //datasourcetype 用于表示三种数据来源
        // 0 , 1, 2 分别表示手工录入技术 中心、材料调整、产品调整
        private void BindMyData(int PageIndex,string  DataSourceType)
        {
            int skipAccount = PageIndex * 10;


            List<VABC_JS_CW_Meterial_P> Query1 = new List<VABC_JS_CW_Meterial_P>();
            List<VABC_JS_CW_Meterial_P> Query2 = new List<VABC_JS_CW_Meterial_P>();
         //   List<VABC_JS_CW_Meterial_P> Query3= new List<VABC_JS_CW_Meterial_P>();

    
            string ProductID = TextBox_Query_ProductID.Text;
            string MeterialID = TextBox_Query_MeterialID.Text;
            switch(DataSourceType)
            {
                case "0" :
                {
                    if(ProductID != "")
                    {
                        Query1 = (from a in db.VABC_JS_CW_Meterial_P
                          where a.来源标识 == "技术中心"  
                          && a.产品ID.StartsWith(ProductID)
                           orderby a.材料编码, a.产品ID
                          select a).ToList();
                    }
                    else
                    {
                        Query1 = (from a in db.VABC_JS_CW_Meterial_P
                          where a.来源标识 == "技术中心"  
                          orderby a.材料编码,a.产品ID
                          select a).ToList();
                    }

                    if(MeterialID != "")
                    {

                        Query2 = (from a in Query1
                                 where a.材料编码.StartsWith(MeterialID)
                                 orderby a.材料编码,a.产品ID
                                  select a).ToList();
                    }
                    else
                    {
                        Query2 = (from a in Query1
                                 select a).ToList();
                    }



                    break;
                }
                case "1":
                {
                    if (ProductID != "")
                    {
                        Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                  where a.来源标识 == "财务手工录入"
                                  && a.产品ID.StartsWith(ProductID)
                                  orderby a.材料编码, a.产品ID
                                  select a).ToList();
                    }
                    else
                    {
                        Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                  where a.来源标识 == "财务手工录入"
                                  orderby a.材料编码, a.产品ID
                                  select a).ToList();
                    }

                    if (MeterialID != "")
                    {

                        Query2 = (from a in Query1
                                  where a.材料编码.StartsWith(MeterialID)
                                  orderby a.材料编码, a.产品ID
                                  select a).ToList();
                    }
                    else
                    {
                        Query2 = (from a in Query1
                                  select a).ToList();
                    }



                    break;
                }
                case "2":
                {
                    if (ProductID != "")
                    {
                        Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                  where a.来源标识 == "半成品领料" && !a.产品ID.StartsWith("215")
                                  && a.产品ID.StartsWith(ProductID)
                                  orderby a.材料编码, a.产品ID
                                  select a).ToList();
                    }
                    else
                    {
                        Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                  where a.来源标识 == "半成品领料" && !a.产品ID.StartsWith("215")
                                  orderby a.材料编码, a.产品ID
                                  select a).ToList();
                    }

                    if (MeterialID != "")
                    {

                        Query2 = (from a in Query1
                                  where a.材料编码.StartsWith(MeterialID)
                                  orderby a.材料编码, a.产品ID
                                  select a).ToList();
                    }
                    else
                    {
                        Query2 = (from a in Query1
                                  select a).ToList();
                    }



                    break;
                }

                case "3":
                {
                    if (ProductID != "")
                    {
                        Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                  where a.来源标识 == "半成品领料" && a.产品ID.StartsWith("215")
                                  && a.产品ID.StartsWith(ProductID)
                                  orderby a.材料编码, a.产品ID
                                  select a).ToList();
                    }
                    else
                    {
                        Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                  where a.来源标识 == "半成品领料" && a.产品ID.StartsWith("215")
                                  orderby a.材料编码, a.产品ID
                                  select a).ToList();
                    }

                    if (MeterialID != "")
                    {

                        Query2 = (from a in Query1
                                  where a.材料编码.StartsWith(MeterialID)
                                  orderby a.材料编码, a.产品ID
                                  select a).ToList();
                    }
                    else
                    {
                        Query2 = (from a in Query1
                                  select a).ToList();
                    }



                    break;
                }

            }


            var Query3 = (from a in Query2
                         join b in db.TABC_JS_ProductInProcess
                         on a.产品ID.ToString() + a.作业中心ID.ToString() equals b.产品ID.ToString() + b.作业中心ID.ToString()
                         into temps
                         from temp in temps.DefaultIfEmpty()
                         orderby a.产品ID, a.材料编码,a.作业中心ID
                         select new
                         {
                             a.ID,
                            
                           
                             a.材料编码,
                             a.材料名称,
                             a.产品ID,
                             a.产品名称,
                             a.作业中心ID,
                             a.计量单位,
                             a.数量,
                             a.金额,
                             a.作业中心名称,
                             中间体=(temp ==null?"<font color=red>无</font>":"有"),
                             可用 = ((temp == null || temp.可用 == false) ? "<font color=red>否</font>" : "是")




                         }).ToList();
            

           var  Query4 = ((from a in Query3 select a).Skip(skipAccount).Take(10)).ToList();



            GridView_Meterial_P.DataSource = Query4;
            GridView_Meterial_P.DataBind();

            var SumMoney = (from a in Query2
                            select a.金额).Sum();
           

            Label_Sum.Text = "当前查询记录的总金额：" + string.Format("{0:N2}", (SumMoney.ToString() == "" ? 0 : SumMoney)) + "元";


        }

    //查询出当前记录信息的总条数。
        private int TotalPageAmount(string DataSourceType)
        {
            int MyQuery = 0;

            List<VABC_JS_CW_Meterial_P> Query1 = new List<VABC_JS_CW_Meterial_P>();

            List<VABC_JS_CW_Meterial_P> Query2 = new List<VABC_JS_CW_Meterial_P>();



            string ProductID = TextBox_Query_ProductID.Text;
            string MeterialID = TextBox_Query_MeterialID.Text;
            switch (DataSourceType)
            {
                case "0":
                    {
                        if (ProductID != "")
                        {
                             Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                                                  where a.来源标识 == "技术中心"
                                                                  && a.产品ID.StartsWith(ProductID)
                                                                  orderby a.材料编码, a.产品ID
                                                                  select a).ToList();
                        }
                        else
                        {
                            Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                                                  where a.来源标识 == "技术中心"
                                                                  orderby a.材料编码, a.产品ID
                                                                  select a).ToList();
                        }

                        if (MeterialID != "")
                        {

                            Query2 = (from a in Query1
                                      where a.材料编码.StartsWith(MeterialID)
                                                                  orderby a.材料编码, a.产品ID
                                                                  select a).ToList();
                        }
                        else
                        {
                            Query2 = (from a in Query1
                                                                  select a).ToList();
                        }



                        break;
                    }
                case "1" :
                    {
                        if (ProductID != "")
                        {
                            Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                      where a.来源标识 == "财务手工录入"
                                      && a.产品ID.StartsWith(ProductID)
                                      orderby a.材料编码, a.产品ID
                                      select a).ToList();
                        }
                        else
                        {
                            Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                      where a.来源标识 == "财务手工录入"
                                      orderby a.材料编码, a.产品ID
                                      select a).ToList();
                        }

                        if (MeterialID != "")
                        {

                            Query2 = (from a in Query1
                                      where a.材料编码.StartsWith(MeterialID)
                                      orderby a.材料编码, a.产品ID
                                      select a).ToList();
                        }
                        else
                        {
                            Query2 = (from a in Query1
                                      select a).ToList();
                        }



                        break;
                    }
                case "2" :
                    {
                        if (ProductID != "")
                        {
                            Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                      where a.来源标识 == "半成品领料"  && !a.产品ID.StartsWith("215")
                                      && a.产品ID.StartsWith(ProductID)
                                      orderby a.材料编码, a.产品ID
                                      select a).ToList();
                        }
                        else
                        {
                            Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                      where a.来源标识 == "半成品领料" && !a.产品ID.StartsWith("215")
                                      orderby a.材料编码, a.产品ID
                                      select a).ToList();
                        }

                        if (MeterialID != "")
                        {

                            Query2 = (from a in Query1
                                      where a.材料编码.StartsWith(MeterialID)
                                      orderby a.材料编码, a.产品ID
                                      select a).ToList();
                        }
                        else
                        {
                            Query2 = (from a in Query1
                                      select a).ToList();
                        }



                        break;
                    }


                case "3":
                    {
                        if (ProductID != "")
                        {
                            Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                      where a.来源标识 == "半成品领料" && a.产品ID.StartsWith("215")
                                      && a.产品ID.StartsWith(ProductID)
                                      orderby a.材料编码, a.产品ID
                                      select a).ToList();
                        }
                        else
                        {
                            Query1 = (from a in db.VABC_JS_CW_Meterial_P
                                      where a.来源标识 == "半成品领料" && a.产品ID.StartsWith("215")
                                      orderby a.材料编码, a.产品ID
                                      select a).ToList();
                        }

                        if (MeterialID != "")
                        {

                            Query2 = (from a in Query1
                                      where a.材料编码.StartsWith(MeterialID)
                                      orderby a.材料编码, a.产品ID
                                      select a).ToList();
                        }
                        else
                        {
                            Query2 = (from a in Query1
                                      select a).ToList();
                        }



                        break;
                    }

            }
            MyQuery = (from a in Query2 select a).Count();

 
            return MyQuery;
        }

        #region 实现翻页功能的代码段
        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount(RadioButtonList_Query.SelectedItem.Value);



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {

                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx,RadioButtonList_Query.SelectedItem.Value);

        }



        #endregion

        protected void Button_Select_Command(object sender, CommandEventArgs e)
        {

            Panel_Main.Visible = true;

            string MyID = e.CommandArgument.ToString();
            var myRow = (from a in db.TABC_CW_Meterial_P
                         where a.ID == Convert.ToInt32(MyID)
                         select a).Single();

            Full_Dropdownlist_Center(myRow.产品ID);


            Current_ID.Value = MyID;
            TextBox_ID.Text = myRow.产品ID;
            TextBox_ProductName.Text = myRow.产品名称;
            TextBox_MeterialID.Text = myRow.材料编码;
            TextBox_MeterialName.Text = myRow.材料名称;
            TextBox_Shuliang.Text = myRow.数量.ToString();
            TextBox_Unit.Text = myRow.计量单位;
            TextBox_JinE.Text = myRow.金额.ToString();
            DropDownList_Center.SelectedValue = myRow.作业中心ID.ToString();

        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {


            var myEditRows = from a in db.TABC_CW_Meterial_P
                            where a.ID ==  Convert.ToInt32(Current_ID.Value)
                            select a;

            if (myEditRows.Count() == 1)
            {
                var myEditRow = myEditRows.Single();
                myEditRow.金额 = Convert.ToDecimal(TextBox_JinE.Text);
                myEditRow.数量 = Convert.ToDecimal(TextBox_Shuliang.Text);
                if (DropDownList_Center.SelectedValue != "")
                {
                    myEditRow.作业中心ID = Convert.ToInt32(DropDownList_Center.SelectedValue);
                }
                else
                {
                    myEditRow.作业中心ID = null;
                }

                try
                {
                    db.SubmitChanges();
                  
                    alert = "<script>alert('保存数据成功');</script>";
                    Panel_Main.Visible = false;
                    int pageIndx = Convert.ToInt32(CurrentPage.Value);
                    BindMyData(pageIndx, RadioButtonList_Query.SelectedItem.Value);

                    Current_ID.Value = "";
                }
                catch
                {
                    
                    alert = "<script>alert('保存数据失败');</script>";
                }
            
            
            
            
            }
        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
           


            TextBox_ID.Text ="";
            TextBox_ProductName.Text = "";
            TextBox_MeterialID.Text = "";
            TextBox_MeterialName.Text ="";
            TextBox_Shuliang.Text = "";
            TextBox_Unit.Text = "";
            TextBox_JinE.Text = "";
            DropDownList_Center.SelectedValue = "";

            Current_ID.Value = "";
            
            Panel_Main.Visible = false;
        }






        private void Full_Dropdownlist_Center(string ProductID)
        {


            DropDownList_Center.Items.Clear();
            DropDownList_Center.Items.Add(new ListItem("未指定", ""));

            var myCenters = (from a in db.TABC_JS_ProductInProcess 
                            join b in db.TABC_JS_CostCenter on a.作业中心ID equals b.作业中心ID
                            where a.产品ID == ProductID && b.停用 == false && a.可用 == true
                            orderby b.作业中心名称
                            select new
                            {
                                a.作业中心ID,
                                b.作业中心名称

                            }).Distinct();

            foreach (var myCenter in myCenters)
            {
                DropDownList_Center.Items.Add(new ListItem(myCenter.作业中心名称, myCenter.作业中心ID.ToString()));


            }

        
        
        
        
        
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {

            int Totals = TotalPageAmount(RadioButtonList_Query.SelectedItem.Value);
            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
            Label_Total.Text = "共" + pages.ToString();
            Label_CurrenPageAndTo.Text = "页，当前第1页，转到";
            BindMyData(0, RadioButtonList_Query.SelectedItem.Value);

        }


        




    }
}
